package site.xuqing.autohelperengine;

import android.accessibilityservice.AccessibilityService;
import android.graphics.Path;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.List;

import site.xuqing.autohelper.bean.StepBean;
import site.xuqing.autohelper.listener.OnStepListener;
import site.xuqing.autohelper.type.ScrollType;
import site.xuqing.autohelper.util.StringUtil;
import site.xuqing.autohelperengine.local.bean.StepLocalBean;
import site.xuqing.autohelperengine.util.StepExecutorImpl;

/**
 * @author xuqing
 */
public class Step {
    /**
     * 步骤的具体执行
     */
    public static void step(AccessibilityService service, StepLocalBean stepBean, StepExecutorImpl stepUtil) throws Exception{
        System.out.println("local step executing...");
        switch (stepBean.getStepType()) {
            case SINGLE_TAP:
                //TAG 这里从100改成了1
                doGesture(service, stepBean, 1L, stepUtil);
                break;
            case SCROLL:
            case LONG_TAP:
                doGesture(service, stepBean, 500L, stepUtil);
                break;
            case UP_SCROLL:
                stepUtil.scroll(service, ScrollType.UP);
                break;
            case DOWN_SCROLL:
                stepUtil.scroll(service, ScrollType.DOWN);
                break;
            case LEFT_SCROLL:
                stepUtil.scroll(service, ScrollType.LEFT);
                break;
            case RIGHT_SCROLL:
                stepUtil.scroll(service, ScrollType.RIGHT);
                break;
            default:
                break;
        }
    }

    public static void step(AccessibilityService service, StepBean stepBean, StepExecutorImpl stepUtil, OnStepListener onStepListener) throws Exception{
        System.out.println(" step executing...");
        switch (stepBean.getStepType()){
            case SINGLE_TAP:
                stepUtil.singleTap(stepUtil.findStepInfoNode(service.getRootInActiveWindow(),stepBean));
                break;
            case DOUBLE_TAP:
                stepUtil.doubleTap(stepUtil.findStepInfoNode(service.getRootInActiveWindow(),stepBean));
                break;
            case SINGLE_FIRST_TAP:
                List<AccessibilityNodeInfo> nodeList = stepUtil.findStepInfoNodes(service.getRootInActiveWindow(), stepBean.getStepInfo());
                if (nodeList.size()>0) {
                    stepUtil.singleTap(nodeList.get(0));
                }else{
                    throw new Exception("未找到节点");
                }
                break;
            case LONG_TAP:
                stepUtil.longTap(stepUtil.findStepInfoNode(service.getRootInActiveWindow(),stepBean));
                break;
            case UP_SCROLL:
                stepUtil.scroll(service, ScrollType.UP);
                break;
            case DOWN_SCROLL:
                stepUtil.scroll(service, ScrollType.DOWN);
                break;
            case LEFT_SCROLL:
                stepUtil.scroll(service, ScrollType.LEFT);
                break;
            case RIGHT_SCROLL:
                stepUtil.scroll(service, ScrollType.RIGHT);
                break;
            case COPY:
                stepUtil.copy(service, stepUtil.findStepInfoNode(service.getRootInActiveWindow(),stepBean));
                break;
            case PASTE:
                if (StringUtil.isEmptyString(stepBean.getPasteText())){
                    stepUtil.paste(stepUtil.findStepInfoNode(service.getRootInActiveWindow(),stepBean));
                }else{
                    stepUtil.paste(stepUtil.findStepInfoNode(service.getRootInActiveWindow(),stepBean),stepBean.getPasteText());
                }
                break;
            case RETURN:
                stepUtil.back(service);
                break;
            case RECT_SINGLE_TAP:
                stepUtil.rectSingleTap(service,stepUtil.findStepInfoNode(service.getRootInActiveWindow(),stepBean));
                break;
            case RECT_LONG_TAP:
                stepUtil.rectLongTap(service,stepUtil.findStepInfoNode(service.getRootInActiveWindow(),stepBean));
                break;
            default:
                stepBean.getStepScript().doStep(service,stepBean,stepUtil,onStepListener);
                break;
        }
    }

    private static void doGesture(AccessibilityService service, StepLocalBean stepLocalBean, Long duration, StepExecutorImpl stepUtil) throws Exception{
        Path p = new Path();
        p.moveTo(stepLocalBean.getStartPosition().x, stepLocalBean.getStartPosition().y);
        if (stepLocalBean.getEndPosition() != null) {
            p.lineTo(stepLocalBean.getEndPosition().x, stepLocalBean.getEndPosition().y);
        }
        stepUtil.doGesture(service,p,duration);
    }
}
